package com.biejiaogroup.librarymanager.service;

import com.biejiaogroup.librarymanager.entity.Book;
import com.biejiaogroup.librarymanager.mapper.BookMapper;
import com.biejiaogroup.librarymanager.util.GetSQLSession;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.util.Vector;

public class BookService {


    /**
     * @param void
     * @return java.util.Vector<com.biejiaogroup.librarymanager.entity.Book>
     * @Description: 查询所有书籍
     * @Author: JesseChan
     * @Date: 2022/5/4
     */
    public Vector<Book> getAll() {
        Vector<Book> v;
        SqlSession session = GetSQLSession.createSQLSession();
        BookMapper bookMapper = session.getMapper(BookMapper.class);
        v = bookMapper.queryAllBook();
        session.close();
        return v; //返回所有书籍
    }

    /**
     * @param bookID
     * @return: boolean
     * @Description: 删除书籍
     * @Author: JesseChan
     * @Date: 2022/5/5
     */
    public boolean deleteBook(Integer bookID) {
        try {
            SqlSession session = GetSQLSession.createSQLSession();
            BookMapper bookMapper = session.getMapper(BookMapper.class);
            bookMapper.deleteBook(bookID);
            session.commit();
            session.close();
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @param book
     * @return: boolean
     * @Description: 添加书籍
     * @Author: JesseChan
     * @Date: 2022/5/5
     */
    public boolean insertBook(Book book) {
        try {
            SqlSession session = GetSQLSession.createSQLSession();
            BookMapper bookMapper = session.getMapper(BookMapper.class);
            bookMapper.insertBook(book);
            session.commit();
            session.close();
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @param book
     * @return: boolean
     * @Description: 修改书籍
     * @Author: JesseChan
     * @Date: 2022/5/5
     */
    public boolean updateBook(Book book)
    {
        try {
            SqlSession session = GetSQLSession.createSQLSession();
            BookMapper bookMapper = session.getMapper(BookMapper.class);
            bookMapper.updateBook(book);
            session.commit();
            session.close();
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @param bookID
     * @return: com.biejiaogroup.librarymanager.entity.Book
     * @Description: 根据书籍ID查询书籍
     * @Author: JesseChan
     * @Date: 2022/5/6
     */
    public Book queryBookByID(Integer bookID)
    {
        Book book;
        SqlSession session = GetSQLSession.createSQLSession();
        BookMapper bookMapper = session.getMapper(BookMapper.class);
        book = bookMapper.queryBookByID(bookID);
        session.close();
        return book;
    }

    /**
     * @param userID
     * @param bookID
     * @return: boolean
     * @Description: 借书
     * @Author: JesseChan
     * @Date: 2022/5/6
     */
    public boolean borrowBook(@Param("belongTo") Integer userID, @Param("bookID") Integer bookID)
    {
        try {
            SqlSession session = GetSQLSession.createSQLSession();
            BookMapper bookMapper = session.getMapper(BookMapper.class);
            bookMapper.borrowBook(userID, bookID);
            session.commit();
            session.close();
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * @param bookID
     * @return: boolean
     * @Description: 还书
     * @Author: JesseChan
     * @Date: 2022/5/6
     */
    public boolean returnBook(Integer bookID)
    {
        try {
            SqlSession session = GetSQLSession.createSQLSession();
            BookMapper bookMapper = session.getMapper(BookMapper.class);
            bookMapper.returnBook(bookID);
            session.commit();
            session.close();
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @param bookName
     * @return: java.util.Vector<com.biejiaogroup.librarymanager.entity.Book>
     * @Description: 根据书名查询书籍
     * @Author: JesseChan
     * @Date: 2022/5/12
     */
    public Vector<Book> queryBookByName(String bookName)
    {
        Vector<Book> books;
        SqlSession session = GetSQLSession.createSQLSession();
        BookMapper bookMapper = session.getMapper(BookMapper.class);
        books = bookMapper.queryBookByName(bookName);
        session.close();
        return books;
    }


}
